package com.example.demo.config;

import com.example.demo.controller.TestWebSocketController;
import com.example.demo.controller.WebSocketController;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.annotation.Resource;

@Configuration
@EnableWebSocket //启动websocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private TestWebSocketController testWebSocketController;
    @Resource
    private WebSocketController webSocketController;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //通过此方法把刚才创建好的Handler类注册到具体的路径上
        //此时浏览器,websocket的请求路径是"/test"的时候,就会调用到TestWebSocketController类里的方法
        //registry.addHandler(testWebSocketController,"/test");
        registry.addHandler(webSocketController,"/WebSocketMessage")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
